-- NewSpeedDungeonsWinView 竞速副本胜利UI

local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local rewardMaxNum = 3 -- 奖励个数最大值

local PASS_TIME_STR = L("lua_code_text_465")
local NEXT_TIME_GAP_STR = L("lua_code_text_69")

ClassRef.VIEW_ROOT_RES = Res.SpeedWinScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
			if self._curRewardIdx and self._curRewardIdx < self._newRewardNum then
				-- 以下：处理同时获得多奖励时候 UI 的展示
				self._curRewardIdx = self._curRewardIdx + 1
				self._curRewardLv = protobuf.enum_id("pb_speeddungeon.REWARD", self._msg.newRewardList[self._curRewardIdx])
				self:refreshContent()
			else
				self:closeView()
			end
		end)
end

-- //奖励档位
-- enum REWARD{
-- 	NONE	= 0;//无
-- 	BRONZE	= 1;//铜
-- 	SILVER	= 2;//银
-- 	GOLD	= 3;//金
-- }
-- message S2C_SpeedDungeonResult {
--   bool isSuccess = 1;
--   int32 target = 2; //roleplayconfig ID
--   int32 finishSeconds = 10;
--   bool newRecord = 20; //如果没有破纪录，则为false
--   repeated REWARD newRewardList = 30; //如果没有新的解锁，则il为n
-- }

-- speedInfo SpeedDungeonInfo
-- msg = S2C_SpeedDungeonResult
function ClassRef:refresh(speedInfo, msg)
	self._curBestRewardLv = speedInfo:getSpeedDungeonBestRewardLv()
	self._newRewardNum = #msg.newRewardList

	if self._newRewardNum > 0 then
		self._curRewardIdx = 1
		self._curRewardLv = protobuf.enum_id("pb_speeddungeon.REWARD", msg.newRewardList[self._curRewardIdx])
	else
		self._curRewardLv = self._curBestRewardLv
	end

	self._speedInfo = speedInfo
	self._msg = msg

	self:refreshContent()
end

function ClassRef:refreshContent()
	local isGet = false
	local cupIconPath, rewards
	local nextRewardLv = self._curRewardLv + 1
	if self._curRewardLv <= 0 then
		cupIconPath = Res.commonUIPath .. "common_img_2.png"
	else
		cupIconPath = self._speedInfo:getRankIcon(self._curRewardLv)
		if self._newRewardNum > 0 then
			-- 有新奖励获得
			local curGrade = self._speedInfo:getGradeByLv(self._curRewardLv)
			rewards = curGrade.rewards
		else
			isGet = true
		end
	end

	local passTime = self._msg.finishSeconds
	local timeStr = string.format(PASS_TIME_STR, TimeUtil.secondsToTime(passTime))
	self._ui.tips_1:setString(timeStr)
	self._ui.icon:loadTexture(cupIconPath, 1)
	self._ui.tips_get:setVisible(isGet)
	self._ui.tips_new:setVisible(self._msg.newRecord)

	self._ui.tips_img_2:setVisible(rewards == nil)
	if rewards then
		self._ui.tips_2:setVisible(false)
		self._ui.tips_img_2:setVisible(false)
		self._ui.tips_3:setVisible(true)
		self._ui.tips_img_3:setVisible(true)

		self:_autoAdjustRewardPanel(#rewards)
		
		for i,reward in ipairs(rewards) do
			local render = IconRender.createWithData( reward, self._ui["icon_" .. i] )
			render:setTipEnable( true )
			render:setTipUnShowOrigin( true )
		end
	else
		self._ui.tips_3:setVisible(false)
		self._ui.tips_img_3:setVisible(false)

		self:_autoAdjustRewardPanel(0)
		
		local descStr
		if nextRewardLv <= self._curBestRewardLv then
			descStr = L("GetAllAward")
		elseif nextRewardLv <= 3 then
			local nextGrade = self._speedInfo:getGradeByLv(nextRewardLv)
			local gap = passTime - nextGrade.time
			descStr = string.format(NEXT_TIME_GAP_STR, gap)
		end

		self._ui.tips_2:setVisible(descStr ~= nil)
		self._ui.tips_img_2:setVisible(descStr ~= nil)
		if descStr then
			self._ui.tips_2:setString(descStr)
		end
	end
end

-- 自动调整奖励panel中奖励posX
function ClassRef:_autoAdjustRewardPanel(visibleNum)
	for i = 1, rewardMaxNum do
		self._ui["icon_" .. i]:setVisible(i <= visibleNum)
	end

	if 1 == visibleNum then
		self._ui.icon_1:setPositionX(self._ui.icon_2:getPositionX())
	elseif 2 == visibleNum then
		local width = self._ui.Panel:getContentSize2()
		self._ui.icon_1:setPositionX( width*0.35 ) -- 常量系数是UI中的posX比例
		self._ui.icon_2:setPositionX( width*0.65 )
	end
end

return ClassRef
